{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-2-12deff5a077f>:15: DeprecationWarning: use options instead of chrome_options\n",
      "  driver = webdriver.Chrome(chrome_options=chrome_options)\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.support.ui import WebDriverWait,Select\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "from selenium.webdriver.common.action_chains import ActionChains\n",
    "from selenium.webdriver.common.keys import Keys\n",
    "import random\n",
    "chrome_options = webdriver.ChromeOptions()\n",
    "# 使用headless无界面浏览器模式\n",
    "# chrome_options.add_argument('--headless') #增加无界面选项\n",
    "chrome_options.add_argument('--disable-gpu') #如果不加这个选项，有时定位会出现问题\n",
    "\n",
    "# 启动浏览器，获取网页源代码\n",
    "driver = webdriver.Chrome(chrome_options=chrome_options)\n",
    "driver.implicitly_wait(10)\n",
    "driver.maximize_window()\n",
    "time.sleep(1)\n",
    "mainUrl = \"https://pc.xuexi.cn/points/my-points.html\"\n",
    "driver.get(mainUrl)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def read():\n",
    "    driver.get(mainUrl)\n",
    "    driver.find_element_by_css_selector('body').send_keys(Keys.ENTER)\n",
    "    # time.sleep(10)\n",
    "    title =driver.find_elements_by_css_selector('[class=\"my-points-card-title\"]')\n",
    "    txt = driver.find_elements_by_css_selector('[class=\"big\"]')\n",
    "    # for i in range(1,7):\n",
    "    if title[1].text == \"我要选读文章\" and txt[1].text == \"去看看\":\n",
    "        print('我要选读文章')\n",
    "        driver.get('https://www.xuexi.cn')\n",
    "        time.sleep(5)\n",
    "        # driver.find_element_by_link_text(\"环球视野\").click()\n",
    "        # time.sleep(5)\n",
    "        # toHandle = driver.window_handles\n",
    "        # print(toHandle)\n",
    "        # driver.switch_to.window(toHandle[1])\n",
    "        read = driver.find_elements_by_css_selector('[class=\"text-link-item-title\"]')\n",
    "        lm = []\n",
    "        for m in range(7):\n",
    "            km = random.randint(17,50)\n",
    "            lm.append(km)\n",
    "        print(lm)\n",
    "        n = 0\n",
    "        for j in lm:\n",
    "            n = n+1\n",
    "            print(j)\n",
    "            read[j].click()\n",
    "            time.sleep(5)\n",
    "            toHandle = driver.window_handles\n",
    "            # driver.switch_to.window(toHandle[1])\n",
    "            driver.switch_to.window(toHandle[n])\n",
    "            driver.execute_script(\"window.scrollTo(0,document.body.scrollHeight)\")\n",
    "            time.sleep(65)\n",
    "            driver.switch_to.window(toHandle[0])\n",
    "        time.sleep(3)\n",
    "        for k in range(n):\n",
    "            c_k = n - k\n",
    "            driver.switch_to.window(toHandle[c_k])\n",
    "            driver.close()\n",
    "        driver.switch_to.window(toHandle[0])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def video():\n",
    "    driver.get(mainUrl)\n",
    "    driver.find_element_by_css_selector('body').send_keys(Keys.ENTER)\n",
    "    time.sleep(10)\n",
    "    title =driver.find_elements_by_css_selector('[class=\"my-points-card-title\"]')\n",
    "    txt = driver.find_elements_by_css_selector('[class=\"big\"]')\n",
    "    if title[2].text == \"视听学习\" and txt[2].text == \"去看看\":\n",
    "        driver.get('https://www.xuexi.cn')\n",
    "        time.sleep(5)\n",
    "        driver.find_element_by_link_text(\"学习电视台\").click()\n",
    "        time.sleep(5)\n",
    "        toHandle = driver.window_handles\n",
    "        print(toHandle)\n",
    "        driver.switch_to.window(toHandle[1])\n",
    "        # driver.current_url\n",
    "        # url = driver.current_url\n",
    "        # print(url)\n",
    "        channels = driver.find_elements_by_css_selector('[class=\"item-text\"]')\n",
    "        while(1):\n",
    "            n =random.randint(0,11)\n",
    "            if n != 3|9|4:\n",
    "                break\n",
    "        channels[n].click()\n",
    "        toHandle = driver.window_handles\n",
    "        driver.execute_script(\"window.scrollTo(0,document.body.scrollHeight)\")\n",
    "        time.sleep(20)\n",
    "        print(toHandle)\n",
    "        driver.switch_to.window(toHandle[2])\n",
    "        video = driver.find_elements_by_css_selector('[class=\"innerPic\"]')\n",
    "        for v in range(1,7):\n",
    "            video[v].click()\n",
    "            time.sleep(2)\n",
    "            toHandle = driver.window_handles\n",
    "            driver.switch_to.window(toHandle[3])\n",
    "            driver.execute_script(\"window.scrollTo(0,document.body.scrollHeight)\")\n",
    "            time.sleep(65)\n",
    "            driver.close()\n",
    "            driver.switch_to.window(toHandle[2])\n",
    "        driver.close()\n",
    "        driver.switch_to.window(toHandle[1])\n",
    "        driver.close()\n",
    "        driver.switch_to.window(toHandle[0])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def answer(driver):\n",
    "    for nr in range(5):\n",
    "        tips = driver.find_element_by_css_selector('[class=\"tips\"]')\n",
    "        tips.click()\n",
    "        time.sleep(2)\n",
    "        tips_txt = driver.find_element_by_css_selector('[class=\"line-feed\"]').text\n",
    "        tips_red = driver.find_elements_by_css_selector('[class=\"line-feed\"]>font')\n",
    "        time.sleep(2)\n",
    "        tips.click()\n",
    "        t_list = []\n",
    "        # ic(nr)\n",
    "        for t in tips_red:\n",
    "            t_list.append(t.text)\n",
    "        t_s = ''.join(t_list)\n",
    "        sel_ans = driver.find_elements_by_css_selector('[class*=\"q-answer choosable\"]')\n",
    "        if sel_ans == []:\n",
    "            print('select is empty')\n",
    "            #视频题        \n",
    "            if t_list[0] == '请观看视频':\n",
    "                inputs = driver.find_elements_by_css_selector('[class=\"blank\"]')\n",
    "                time.sleep(1)\n",
    "                inputs.send_keys('不知道')\n",
    "                \n",
    "            #填空题\n",
    "            else:\n",
    "                inputs = driver.find_elements_by_css_selector('[class=\"blank\"]')\n",
    "                for in_ans in range(len(inputs)):\n",
    "                    inputs[in_ans].click()\n",
    "                    time.sleep(1)\n",
    "                    inputs[in_ans].send_keys(t_list[in_ans])\n",
    "                    # ActionChains(driver).move_to_element(inputs[in_ans]).send_keys(t_list[in_ans]).perform()\n",
    "        \n",
    "        else:\n",
    "            #选择题\n",
    "            import re\n",
    "            for a in sel_ans:\n",
    "                pattern = re.match( r'^[A-Z]\\.\\s(.*)',a.text, re.M|re.I)\n",
    "                a_t = pattern.group(1)\n",
    "                if re.search(r'.*(%s)'%a_t,t_s):\n",
    "                    a.click()\n",
    "                    # time.sleep(2)\n",
    "                    # ic('yes')\n",
    "                time.sleep(2)\n",
    "        try:\n",
    "            driver.find_element_by_css_selector('[class=\"ant-btn submit-btn ant-btn-primary ant-btn-background-ghost\"]').click()\n",
    "        except NoSuchElementException as error:\n",
    "            driver.find_element_by_css_selector('[class=\"ant-btn next-btn ant-btn-primary\"]').click()\n",
    "        time.sleep(5)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 每日答题\n",
    "def daily_answer():\n",
    "    driver.get(mainUrl)\n",
    "    title =driver.find_elements_by_css_selector('[class=\"my-points-card-title\"]')\n",
    "    txt = driver.find_elements_by_css_selector('[class=\"big\"]')\n",
    "    if title[4].text == \"每日答题\" and txt[4].text == \"去答题\": \n",
    "        driver.get('https://pc.xuexi.cn/points/exam-practice.html')\n",
    "        time.sleep(5)\n",
    "        answer(driver)\n",
    "        # for nr in range(5):\n",
    "        #     tips = driver.find_element_by_css_selector('[class=\"tips\"]')\n",
    "        #     tips.click()\n",
    "        #     time.sleep(2)\n",
    "        #     tips_txt = driver.find_element_by_css_selector('[class=\"line-feed\"]').text\n",
    "        #     print(tips_txt)\n",
    "        #     #视频题        \n",
    "        #     if tips_txt == '请观看视频':\n",
    "        #         inputs = driver.find_elements_by_css_selector('[class=\"blank\"]')\n",
    "        #         time.sleep(1)\n",
    "        #         inputs.send_keys('不知道')\n",
    "        #     #     driver.refresh()\n",
    "        #     #     time.sleep(2)\n",
    "        #     #     driver.find_element_by_css_selector('[class=\"exam-practice\"]').send_keys(Keys.ENTER)\n",
    "        #     #     nr = 0\n",
    "        #     #     continue\n",
    "        #     tips_red = driver.find_elements_by_css_selector('[class=\"line-feed\"]>font')\n",
    "        #     time.sleep(2)\n",
    "        #     tips.click()\n",
    "        #     t_list = []\n",
    "        #     # ic(nr)\n",
    "        #     for t in tips_red:\n",
    "        #         t_list.append(t.text)\n",
    "        #     t_s = ''.join(t_list)\n",
    "        #     sel_ans = driver.find_elements_by_css_selector('[class*=\"q-answer choosable\"]')\n",
    "        #     #填空题\n",
    "        #     if sel_ans == []:\n",
    "        #         inputs = driver.find_elements_by_css_selector('[class=\"blank\"]')\n",
    "        #         for in_ans in range(len(inputs)):\n",
    "        #             inputs[in_ans].click()\n",
    "        #             time.sleep(1)\n",
    "        #             inputs[in_ans].send_keys(t_list[in_ans])\n",
    "        #             # ActionChains(driver).move_to_element(inputs[in_ans]).send_keys(t_list[in_ans]).perform()\n",
    "            \n",
    "        #     else:\n",
    "        #         #选择题\n",
    "        #         import re\n",
    "        #         for a in sel_ans:\n",
    "        #             pattern = re.match( r'^[A-Z]\\.\\s(.*)',a.text, re.M|re.I)\n",
    "        #             a_t = pattern.group(1)\n",
    "        #             if re.search(r'.*(%s)'%a_t,t_s):\n",
    "        #                 a.click()\n",
    "        #                 # time.sleep(2)\n",
    "        #                 # ic('yes')\n",
    "        #             time.sleep(2)\n",
    "                \n",
    "\n",
    "        #     driver.find_element_by_css_selector('[class=\"ant-btn next-btn ant-btn-primary\"]').click()\n",
    "        #     time.sleep(5)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 专项答题\n",
    "from selenium.common.exceptions import NoSuchElementException\n",
    "def special():\n",
    "    driver.get(mainUrl)\n",
    "    title =driver.find_elements_by_css_selector('[class=\"my-points-card-title\"]')\n",
    "    txt = driver.find_elements_by_css_selector('[class=\"big\"]')\n",
    "    if title[5].text == \"专项答题\" and txt[5].text == \"去答题\": \n",
    "        driver.get('https://pc.xuexi.cn/points/exam-paper-list.html')\n",
    "        contiune_bt = driver.find_element_by_css_selector('[class=\"ant-btn button ant-btn-primary ant-btn-background-ghost\"]')\n",
    "        if contiune_bt.text == '继续答题':\n",
    "            contiune_bt.click()\n",
    "            time.sleep(5)\n",
    "            url = driver.current_url\n",
    "            driver.get(url)\n",
    "            answer(driver)\n",
    "            driver.get(mainUrl)\n",
    "        else:\n",
    "            try:\n",
    "                red_bt = driver.find_element_by_css_selector('[class=\"ant-btn button ant-btn-primary\"]')\n",
    "                red_bt.click()\n",
    "                time.sleep(5)\n",
    "                # driver.current_url\n",
    "                url = driver.current_url\n",
    "                driver.get(url)\n",
    "                answer(driver)\n",
    "                driver.get(mainUrl)\n",
    "            except NoSuchElementException as error:\n",
    "                print('no new qus')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我要选读文章\n",
      "[39, 26, 45, 39, 20, 21, 27]\n",
      "39\n",
      "26\n",
      "45\n",
      "39\n",
      "20\n",
      "21\n",
      "27\n",
      "['CDwindow-597BC68F865E9ED173FDF2E96D302F75', 'CDwindow-7CEF56521F3603476B013D2567501E3D']\n",
      "['CDwindow-597BC68F865E9ED173FDF2E96D302F75', 'CDwindow-7CEF56521F3603476B013D2567501E3D', 'CDwindow-5A27865C35EDA3952B5F6D84CDDA0EB9']\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    read()\n",
    "    video()\n",
    "    special()\n",
    "    daily_answer()\n",
    "except:\n",
    "    while (1):\n",
    "        read()\n",
    "        video()\n",
    "        special()\n",
    "        daily_answer()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "a1bc945abcc4336ce0ee9b8adf13f2e53d2ca1cbbefef65deaebdc6378592e20"
  },
  "kernelspec": {
   "display_name": "Python 3.9.9 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
